package de.szut.sudoku.gui.menu;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import de.szut.internalorganizer.InternalLoader;
import de.szut.sudoku.gui.MainFrame;
import de.szut.sudoku.main.GameController;

/**
 * SubMenu of "Game"
 * 
 * @author dqi12huisinga
 * 
 */
@SuppressWarnings("serial")
public class GameSubMenu extends JMenu {
	/**
	 * Instantiates the SubMenu when called
	 */
	public GameSubMenu() {
		// Adds this SubMenu to the translatable components
		MainFrame.getInstance().getlContainer()
				.addTranslatableComponent("gui.menu.GameSubMenu", this);

		JMenuItem mntmNewGame = new JMenuItem();
		// Adds the option "New game" to the translatable components
		MainFrame
				.getInstance()
				.getlContainer()
				.addTranslatableComponent("gui.menu.GameSubMenu.newgame",
						mntmNewGame);
		// Resets the field when new game is clicked
		mntmNewGame.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				MainFrame.getInstance().resetField();
			}

		});
		add(mntmNewGame);

		// Adds the option "Load game" to the translatable components
		JMenuItem mntmLoadGame = new JMenuItem();
		MainFrame
				.getInstance()
				.getlContainer()
				.addTranslatableComponent("gui.menu.GameSubMenu.loadgame",
						mntmLoadGame);
		// Loads a sudokufield when "load game" is clicked
		mntmLoadGame.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser fileChooser = new JFileChooser();
				int option = fileChooser.showOpenDialog(null);
				if (option == JFileChooser.APPROVE_OPTION) {
					InternalLoader loader = new InternalLoader();
					loader.setFile(fileChooser.getSelectedFile());
					try {
						String[][][] loadedData = loader.loadData("", "Sudoku");
						GameController.getInstance().getGame()
								.loadGame(loadedData);
					} catch (IOException e) {
						e.printStackTrace();
					}

				}
			}

		});
		add(mntmLoadGame);
	}
}
